package jiang;
public class VolatileTest5 {
    VolatileTest5_Resource resource = new VolatileTest5_Resource();
    public static void main(String[] args) {
        for (int i=0; i<10000000; i++) {
            VolatileTest5 v = new VolatileTest5();
            Thread t1 = new Thread(new VolatileTest5_A(v));
            Thread t2 = new Thread(new VolatileTest5_B(v));
            t1.start();
            t2.start();
        }
    }
}
class VolatileTest5_Resource {
    boolean b = false;
    int a = 0;
}
class VolatileTest5_A implements Runnable {
    volatile VolatileTest5 r = null;

    public VolatileTest5_A(VolatileTest5 a) {
        r = a;
    }
    public void run() {
        r.resource.a = 1;
        r.resource.b = true;
    }
}
class VolatileTest5_B implements Runnable {
    volatile VolatileTest5 r = null;
    public VolatileTest5_B(VolatileTest5 a) {
        r = a;
    }
    public void run() {
        while (!r.resource.b) {
            Thread.yield();
        }
        int temp = r.resource.a;
        if (temp == 0) {
            System.out.println("出现CPU指令重排");
        }
    }
}
